﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using RBS.Admin.Controllers;
using RBS.Common;
using RBS.Common.JsonEntity;
using Webdiyer.WebControls.Mvc;

namespace RBS.Admin.Areas.DeedAdmin.Controllers
{
    public class CouponsController : BaseController
    {
        //
        // GET: /Deed/Coupons/ 
        public ActionResult AdvertitAuditList(int status =2 ,int id = 1, string kword = null)
        {
            const int pageSize = 10;

            var bll = new RBS.Core.BLL.P_Advertis_Payment();
            // var admin = GetAdminInfo(); //取得当前管理员信息
            if (kword != null) kword = kword.Replace("'", "");
            var strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(kword))
            {
                //strTemp.Append(" and (H_UID like  '%" + kword + "%' or H_UserName like '%" + kword + "%')");
            }
            string strWhere = " and DeedType=3 and Deedstatus ="+status + strTemp.ToString();
            const string orderby = " id  desc";
            var startIndex = id;

            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数 
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.P_Advertis_Payment>(dt);
            PagedList<RBS.Core.Model.P_Advertis_Payment> arts = null;
            if (list != null)
                arts = new PagedList<RBS.Core.Model.P_Advertis_Payment>(list, id, pageSize, totalItems);
            else
            {
                list = new List<RBS.Core.Model.P_Advertis_Payment>();
                arts = new PagedList<RBS.Core.Model.P_Advertis_Payment>(list, id, pageSize, totalItems);
            }
            return View(arts);
        }

        #region 活动审核
        
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult CheckAuditList(string ids, string returnUrl)
        {
            var ret = new JsonReturnMessages();

            try
            {
                foreach (var id in ids.TrimEnd(',').Split(',').ToList())
                {

                    var manager = base.GetAdminInfo();
                    var checkBll = new RBS.Core.BLL.P_Advertis_Checklist();
                    var checkModel = new RBS.Core.Model.P_Advertis_Checklist
                        {
                            AP_Id = int.Parse(id),
                            AuditTime = DateTime.Now,
                            ReviewerID = manager.id,
                            Status = (int) RBSEnums.DeedStatus.Ok
                        };  
                    var bll = new RBS.Core.BLL.P_Advertis_Payment();
                      bll.UpdateStatus(int.Parse(id), checkModel.Status);
                    returnUrl = "AdvertitAuditList";
                }
            }
            catch (Exception exception)
            {
                ret.IsSuccess = false;
                ret.Msg = "操作失败！";
                return Json(ret);
            }

            ret.IsSuccess = true;
            ret.Msg = "操作成功";


            ret.Data = returnUrl;
            return Json(ret);
        }
        #endregion

        #region 酒店活动 
        /// <summary>
        /// 酒店活动列表
        /// </summary>
        /// <returns></returns>
        public ActionResult CouponsList(int hid,int id=1 , string kword = null)
        {
            return View(PaymentList(hid, id, kword));
        }
        public PagedList<RBS.Core.Model.P_Advertis_Payment> PaymentList(int hid = 0, int id = 1, string kword = null)
        {
            const int pageSize = 10; 
            var bll = new RBS.Core.BLL.P_Advertis_Payment();
            // var admin = GetAdminInfo(); //取得当前管理员信息
            if (kword != null) kword = kword.Replace("'", "");
            var strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(kword))
            {
                //strTemp.Append(" and (H_UID like  '%" + kword + "%' or H_UserName like '%" + kword + "%')");
            }
            string strWhere = " and HotelId=" + hid + strTemp.ToString();
            const string orderby = " id  desc";
            var startIndex = id; 
            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数 
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.P_Advertis_Payment>(dt);
            PagedList<RBS.Core.Model.P_Advertis_Payment> arts = null;
            if (list != null)
                arts = new PagedList<RBS.Core.Model.P_Advertis_Payment>(list, id, pageSize, totalItems);
            else
            {
                list = new List<RBS.Core.Model.P_Advertis_Payment>();
                arts = new PagedList<RBS.Core.Model.P_Advertis_Payment>(list, id, pageSize, totalItems);
            }
            return arts;
        }
        #endregion
    }
}
